﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace QYWebSiteBN
{
    public partial class Form_ContentRuleList : Form
    {
        public Form_ContentRuleList()
        {
            InitializeComponent();
            UpdateList();
        }

        public void UpdateList()
        {

            lbRule.Items.Clear();
            for (int i = 0; i < TranslateContentRule.Count; i++)
            {
                lbRule.Items.Add(TranslateContentRule.Get(i).Name);
            }
        }

        private void btnRuleAdd_Click(object sender, EventArgs e)
        {
            Form_ContentRule fcontentrole = new Form_ContentRule();
            fcontentrole.Owner = this;
            fcontentrole.ShowDialog();
        }



        private void btnTestDB_Click(object sender, EventArgs e)
        {

            check();
            string connStr = String.Format("server={0};user id={1}; password={2}; database={3}; pooling=false;port=3306", txtDBAddress.Text.Trim(),txtDBUser.Text.Trim(), txtDBPassword.Text.Trim(), txtDBServer.Text.Trim());
            MySqlConnection  myConn = new MySqlConnection(connStr);
            MySql.Data.MySqlClient.MySqlConnection conn = new MySql.Data.MySqlClient.MySqlConnection(connStr);
            conn.Open();
            if (conn.State.ToString()=="Open")
            {
                MessageBox.Show("测试成功!");
            }
            else
            {
                MessageBox.Show("测试失败!");
            }

            conn.Close();

        }

        private void btTestQuery_Click(object sender, EventArgs e)
        {
            check();
     
                string connStr = String.Format("server={0};user id={1}; password={2}; database={3}; pooling=false;port=3306", txtDBAddress.Text.Trim(),txtDBUser.Text.Trim(), txtDBPassword.Text.Trim(), txtDBServer.Text.Trim());

                string mySelectQuery = txtSQLString.Text;
                MySqlConnection myConnection = new MySqlConnection(connStr);
                MySqlCommand myCommand = new MySqlCommand(mySelectQuery,myConnection);
                myConnection.Open();
                MySqlDataReader myReader;
                myReader = myCommand.ExecuteReader();
                // Always call Read before accessing data.
                int i=0;
                while (myReader.Read()&&(i<=5)) {
                    lbResult.Items.Add(string.Format("id:{0},content:{1}", myReader.GetInt32(0), myReader.GetString(1)));
                    i++;
                }
                // always call Close when done reading.
                myReader.Close();
                // Close the connection when done with it.
                myConnection.Close();


               
           


        }

        private void check()
        {
            if (string.IsNullOrEmpty(txtDBAddress.Text))
            {
                MessageBox.Show("数据地址不可为空！");
                txtDBAddress.Focus();
                return;
            }
            if (string.IsNullOrEmpty(txtDBUser.Text))
            {
                MessageBox.Show("数据用户不可为空！");

                txtDBUser.Focus();
                return;
            }
            if (string.IsNullOrEmpty(txtDBPassword.Text))
            {
                MessageBox.Show("数据密码不可为空！");
                txtDBPassword.Focus();
                return;
            }
            if (string.IsNullOrEmpty(txtDBPort.Text))
            {
                MessageBox.Show("数据端口不可为空！");
                txtDBPort.Focus();
                return;
            }
            if (string.IsNullOrEmpty(txtDBServer.Text))
            {
                MessageBox.Show("数据库不可以为空!");
                txtDBServer.Focus();
                return;
            }
        }

        private void btnRuleSave_Click(object sender, EventArgs e)
        {

        }

        private void lbRule_DoubleClick(object sender, EventArgs e)
        {
            if (lbRule.SelectedItem != null)
            {
                Form_ContentRule fcontentrole = new Form_ContentRule(lbRule.SelectedIndex);
                fcontentrole.Owner = this;
                fcontentrole.ShowDialog();
            }
        }

        private void btnRuleEdit_Click(object sender, EventArgs e)
        {
            if (lbRule.SelectedItem != null)
            {
                Form_ContentRule fcontentrole = new Form_ContentRule(lbRule.SelectedIndex);
                fcontentrole.Owner = this;
                fcontentrole.ShowDialog();
            }

        }

        private void btnRuleDelete_Click(object sender, EventArgs e)
        {
           
                if (lbRule.SelectedItem != null)
                {
                    DialogResult result = MessageBox.Show("确认对选中的规则进行删除操作?", "确认信息", MessageBoxButtons.YesNo);
                    if (result == System.Windows.Forms.DialogResult.Yes)
                    {
                    TranslateContentRule.Delete(lbRule.SelectedIndex);
                    UpdateList();
                }
            }
        }

        private void btnRuleClare_Click(object sender, EventArgs e)
        {
             DialogResult result = MessageBox.Show("确认对全部规则进行删除操作?", "确认信息", MessageBoxButtons.YesNo);
             if (result == System.Windows.Forms.DialogResult.Yes)
             {
                 

             }
        }
    }
}
